import 'dart:convert';

import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_quality_model.dart';

class FactoryQualityViewModel extends BaseViewModel {
  FactoryQualityModel model;
  List<FactoryQualityOptionModel> selectModels;
  FactoryQualityOptionModel selectDaoModel;
  FactoryQualityOptionModel clickDaoModel;
  TextEditingController zujianController = TextEditingController();
  TextEditingController weichaController = TextEditingController();
  TextEditingController xunjianController = TextEditingController();
  FactoryQualityViewModel() {
    selectModels = List<FactoryQualityOptionModel>();
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.post<FactoryQualityModel>('output/quality-one', null,
        onSuccess: (data) {
      if (null != data) {
        model = data;
       zujianController.text=model.zujian;
       weichaController.text=model.weicha;
       xunjianController.text=model.xunjian;
        updateSelectModels();
        this.setState(ViewState.Success);
      } else {
        model = FactoryQualityModel();
        this.setState(ViewState.Success);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void onSellsSelectChip(
      BuildContext context, FactoryQualityOptionModel model) {
    if (model.check) {
      selectDaoModel = FactoryQualityOptionModel(
        id: model.daoId,
      );
      clickDaoModel = model;

      onSetDaoChipStatus(selectDaoModel);
      Scaffold.of(context).openEndDrawer();

      return;
    }
    if (model.daoId.isEmpty) {
      clickDaoModel = model;
      cancelDaoSelect();
      Scaffold.of(context).openEndDrawer();
      return;
    }
    model.check = !model.check;
    notifyListeners();
    updateSelectModels();
  }

  void onProCatesSelectChip(
      BuildContext context, FactoryQualityOptionModel model) {
    if (model.check) {
      selectDaoModel = FactoryQualityOptionModel(
        id: model.daoId,
      );
      clickDaoModel = model;
      onSetDaoChipStatus(selectDaoModel);
      Scaffold.of(context).openEndDrawer();

      return;
    }
    if (model.daoId.isEmpty) {
      clickDaoModel = model;
      cancelDaoSelect();
      Scaffold.of(context).openEndDrawer();
      return;
    }
    this.model.proCates.forEach((element) {
      element.check = false;
    });
    model.check = true;
    notifyListeners();
    updateSelectModels();
  }

  void delChip(BuildContext context, FactoryQualityOptionModel model) {
      selectModels.remove(model);
      model.check=false;
      updateSelectModels();
  }

  void onSetDaoChipStatus(FactoryQualityOptionModel model) {
    this.model.daos.forEach((element) {
      if (element.id == model.id) {
        element.check = true;
      } else {
        element.check = false;
      }
    });

    notifyListeners();
  }

  void onSelectDaoChip(FactoryQualityOptionModel model) {
    this.model.daos.forEach((element) {
      element.check = false;
    });
    model.check = true;
    notifyListeners();
  }

  void updateSelectModels() {
    selectModels.clear();
    model.sellCates.forEach((element) {
      if (element.check) {
        selectModels.add(element);
      }
    });
    model.proCates.forEach((element) {
      if (element.check) {
        selectModels.add(element);
      }
    });
    notifyListeners();
  }

  void cancelDaoSelect() {
    model.daos.forEach((element) {
      element.check = false;
    });
    notifyListeners();
  }

  void postDaoApi(BuildContext context) {
    if (_getSelectDaoID().isEmpty) {
      FLToast.showText(text: '请选择道数');
      return;
    }
    var dismiss = FLToast.loading(text: '正在设置...');
    Http.instance.post(
        'output/quality-check',
        {
          'cateId': clickDaoModel.id,
          'daoId': _getSelectDaoID(),
        },
        onSuccess: (data) {
          FLToast.showSuccess(text: '设置成功');

          Navigator.pop(context);

          requestApi();
        },
        onError: (error) {},
        onFail: (fail) {},
        complete: () {
          dismiss();
        });
  }

  String _getSelectDaoID() {
    String daoId = '';
    this.model.daos.forEach((element) {
      if (element.check) {
        daoId = element.id;
      }
    });
    return daoId;
  }

  List _getSelectSellsIds() {
    List ids = List();
    model.sellCates.forEach((element) {
      if (element.check) {
        ids.add(element.id);
      }
    });
    return ids;
  }

  List _getSelectProCatesIds() {
    List ids = List();
    model.proCates.forEach((element) {
      if (element.check) {
        ids.add(element.id);
      }
    });
    return ids;
  }

  void postApi(BuildContext context, Function callback) {
    if (_getSelectSellsIds().length == 0) {
      FLToast.showText(text: '请选择产品类型，可多选（必选）');
      return;
    }
    if (zujianController.text.isEmpty) {
      FLToast.showText(text: '请输入组检人数');
      return;
    }
    if (weichaController.text.isEmpty) {
      FLToast.showText(text: '请输入尾查人数');
      return;
    }
    if (xunjianController.text.isEmpty) {
      FLToast.showText(text: '请输入巡检人数');
      return;
    }
    var dismiss = FLToast.loading(text: '正在保存...');
    Http.instance.post(
        'output/quality-save',
        {
          'proCates': jsonEncode(_getSelectProCatesIds()),
          'sellCates': jsonEncode(_getSelectSellsIds()),
          'zujian': zujianController.text,
          'weicha': weichaController.text,
          'xunjian': xunjianController.text
        },
        onSuccess: (data) {
          FLToast.showSuccess(text: '保存成功');
          callback();
          Navigator.pop(context);
        },
        onError: (error) {},
        onFail: (fail) {},
        complete: () {
          dismiss();
        });
  }
}
